package generator.dao.impl;

import cn.hutool.db.handler.BeanListHandler;
import cn.hutool.db.sql.SqlExecutor;
import generator.dao.IColumnDao;
import generator.model.Column;
import generator.util.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author angel.bin@aliyun.com
 * @since 2020-10-05 13:38
 */
public class ColumnDaoImpl implements IColumnDao {

    @Override
    public List<Column> selectColumns(String tableName) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select column_name name, column_default defaultValue, is_nullable isNull, " +
                "data_type dataType, column_type type, column_comment comment, column_key 'key' " +
                "from information_schema.columns where table_name = ? and table_schema =(select database()) " +
                "order by ordinal_position";
        List<Column> columns = SqlExecutor.query(connection, sql, BeanListHandler.create(Column.class), tableName);
        JdbcUtils.close();
        return columns;
    }
}
